
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "examples.h"
#include "xrn_crypto.h"
#include "xrn_core.h"
#include "lorem_ipsum.h"

    /////////////////////////////////////////////////////
    //                   Introduction                  //
    /////////////////////////////////////////////////////
    // This code example shows how to encode and       //
    // decode a stream of bytes.                       //
    //                                                 //
    // Furthermore, data integrity checks are not      //
    // included in the encoding algorithm.             //
    /////////////////////////////////////////////////////

int xrn_encode_bytes_wrapper_example( uint8_t ** encoded_bytes, uint64_t * num_encoded_bytes
 )
{

    //////////////////////////
    // Variable definitions //
    //////////////////////////

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    //////////////////////////
    // Initialize variables //
    //////////////////////////

    // Initialize default run time settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    // Default settings can be changed, for example
    // settings.crc_bytes = 1 ;
    // settings.cnt_bytes = 8 ;
    // settings.state_size = 122 ;
    // strcpy(settings.rnd_str,"my_string") ;

    *encoded_bytes = ( uint8_t * ) malloc( sizeof( uint8_t ) * strlen( LOREM_IPSUM_STR ) );
    if ( *encoded_bytes == NULL ) {
        return -1;
    }

    // encoding settings
    settings.encoding_settings.i_settings.mode = no_encoding;
    settings.encoding_settings.o_settings.mode = std_parameters;

    // some alternative settings can be used such as
    // settings.encoding_settings.o_settings.mode = std_parameters_plus_password; 
    // strcpy ( "my_password", settings.encoding_settings.o_settings.password );
    // settings.encoding_settings.o_settings.password_length = strlen("my_password");

    if ( XSUCCESS != xrn_decode_encode_bytes_wrapper( ( uint8_t * ) LOREM_IPSUM_STR,    // the input bytes
                                                      *encoded_bytes,   // the bytes to encode
                                                      strlen( LOREM_IPSUM_STR ),        // the length of bytes
                                                      settings  // the run time settings
          ) ) {

        free( *encoded_bytes );
        return -1;

    }

    return 0;
}

int xrn_decode_bytes_wrapper_example( uint8_t * input_encoded_bytes, uint64_t num_encoded_bytes, uint8_t ** decoded_bytes
 )
{

    //////////////////////////
    // Variable definitions //
    //////////////////////////

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    //////////////////////////
    // Initialize variables //
    //////////////////////////

    // Initialize default run time settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    *decoded_bytes = ( uint8_t * ) malloc( sizeof( uint8_t ) * strlen( LOREM_IPSUM_STR ) );
    if ( *decoded_bytes == NULL ) {
        return -1;
    }

    // Default settings can be changed, for example
    // settings.crc_bytes = 1 ;
    // settings.cnt_bytes = 8 ;
    // settings.state_size = 122 ;
    // strcpy(settings.rnd_str,"my_string") ;

    // encoding settings
    settings.encoding_settings.o_settings.mode = no_encoding;
    settings.encoding_settings.i_settings.mode = std_parameters;

    if ( XSUCCESS != xrn_decode_encode_bytes_wrapper( input_encoded_bytes,      // input bytes for the encoded file
                                                      *decoded_bytes,   // output bytes for the decoded file
                                                      strlen( LOREM_IPSUM_STR ),        // the length of bytes
                                                      settings  // runtime settings
          ) ) {

        free( *decoded_bytes );
        return -1;

    }

    return 0;
}

int encoding_decoding_bytes_wrapper(
 )
{

    uint8_t *encoded_bytes;
    uint8_t *decoded_bytes;
    uint64_t num_encoded_bytes;

    num_encoded_bytes = 0;
    FILE *fp;

    /////////////
    // Logging //
    /////////////

    // Set the default logging file streams
    // errors -> stderr
    // warnings -> stdout
    // notifications -> stdout
    // debug -> stdout

    xrn_set_default_log(  );

    // To change the default logging you can use the functions below,
    // where fp is the opened file pointer
    // 
    // xrn_change_error_log( fp )
    // xrn_change_warning_log( fp )
    // xrn_change_notification_log( fp )
    // xrn_change_debug_log( fp )

    printf( "encoding... \n" );
    if ( 0 != xrn_encode_bytes_wrapper_example( &encoded_bytes, &num_encoded_bytes ) ) {
        printf( "error in encoding \n" );
        return -1;
    }

    printf( "decoding... \n" );
    if ( 0 != xrn_decode_bytes_wrapper_example( encoded_bytes, num_encoded_bytes, &decoded_bytes ) ) {
        free( encoded_bytes );
        printf( "error in decoding \n" );
        return -1;
    }

    fp = fopen( "decoded.txt", "wb" );
    if ( fp == NULL ) {
        free( decoded_bytes );
        free( encoded_bytes );
        return -1;
    }

    if ( 1 != fwrite( decoded_bytes, strlen( LOREM_IPSUM_STR ), 1, fp ) ) {
        free( decoded_bytes );
        free( encoded_bytes );
        fclose( fp );
        return -1;
    }

    free( decoded_bytes );
    free( encoded_bytes );
    fclose( fp );
    printf( "decoded file in cache/examples/exec/decoded.txt\n" );

    return 0;

}

